package com.example.petback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petback.mapper.UserMapper;
import com.example.petback.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<UserModel> getAllUsers(){
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        return userMapper.selectList(queryWrapper);
    }

    public void updatePasswordById(String username,String password){
        UserModel userModel = userMapper.selectById(username);
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",username);
        updateWrapper.set("password",password);
        userMapper.update(userModel,updateWrapper);
    }

    public void updateStatusById(String username){
        UserModel userModel = userMapper.selectById(username);
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",username);
        Integer status = userModel.getStatus();
        if(status == 0){
            updateWrapper.set("status",1);
        }
        else{
            updateWrapper.set("status",0);
        }
        userMapper.update(userModel,updateWrapper);
    }

    public void deleteUserById(String username){
        userMapper.deleteById(username);
    }
}
